<?php

namespace App\Http\Controllers\Member;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Member\Withdraw;
use App\Models\Member\BankLib;
use App\Models\Member\DealRecord;
use Config, DB, Auth;

class WithdrawController extends Controller
{
    // 提现记录
    public function lists(Request $request)
    {
        # 提现金额 / 提现时间 / 提现状态 / 拒绝原因(小字 , 只有已拒绝的才显示) ==== 提现状态对应的文字
        $user = Auth::user()->toArray();
        $Withdraw = new Withdraw;
        return $Withdraw->lists($user['id']);
    }

    // 提现界面 - 选择银行卡 传递参数调用这个方法
    public function withdrawPage(Request $request)
    {
        # 默认银行卡信息 - 余额 / 如果有传递的银行卡 获取 传递的银行卡信息
        $user = Auth::user()->toArray();
        $BankLib = new BankLib;
        $bankId = empty($request['id']) ? $user['bank_id'] : $request['id'];
        $bank = $BankLib->bankInfo($bankId);
        $info_msg = '提现界面数据加载成功';
        if ($bank['code'] != 20000) {
            $info_msg = $bank['info'];
        } else {
            if ($bank['data']['user_id'] != $user['id']) {
                $info_msg = '这不是你的银行卡, 请更换银行卡!';
                $bank['data'] = null;
            }
        }
        return $this->successClient(['bank' => $bank['data'], 'gold' => $user['gold'], 'true_name' => $user['true_name']], $info_msg); 
    }

    // 提现功能
    public function withdraw(Request $request)
    {
        $bankId = $request['id'];
        $withdraw = $request['withdraw']*100;// 单位元
        $user = Auth::user()->toArray();
        $bank = $this->checkWithdraw($user, $bankId, $withdraw);
        // return $bank;
        if ($bank['code'] != 20000) {
            return $bank;
        }
        // 准备数据
        $dealRecord = $this->dealRecord($withdraw, $user);
        $withdrawRecord = $this->withdrawRecord($withdraw, $user, $bank['data']);
        // 插入数据
        $insert = $this->insertWithdraw($dealRecord, $withdrawRecord);
        return $insert;
    }

    private function insertWithdraw($dealRecord, $withdrawRecord)
    {
        // 准备提现记录数据 并 插入
        $Withdraw = new Withdraw;
        DB::beginTransaction();
        try {
            $insertWithdraw = $Withdraw->insertWithdraw($withdrawRecord);
            if ($insertWithdraw['code'] != 20000) {
                return $insertWithdraw;
            }
            $withdrawId = $insertWithdraw['data'];
            // 插入交易记录
            $DealRecord = new DealRecord;
            $dealRecord['deal_item_id'] = $withdrawId;
            $insertDealRecord = $DealRecord->deductMoney($dealRecord);
            if ($insertDealRecord['code'] == 20000) {
                DB::commit();
            }
            return $insertDealRecord;
        } catch (\Exception $e) {
            DB::rollBack();
            return $e->getLine().$e->getMessage();
        }
    }

    // 准备提现交易记录数据
    private function dealRecord($withdraw, $user)
    {
        $dealGold = [];
        $dealGold['store_id'] = $user['store_id'];
        $dealGold['user_id'] = $user['id'];
        $dealGold['status'] = 1;
        $dealGold['deal_item_id'] = 0;
        $dealGold['order_type'] = 2;
        $dealGold['jm_type'] = 1;
        $dealGold['deal_type'] = 4;
        $dealGold['grade'] = $user['grade'];
        $dealGold['amount'] = $withdraw;
        $dealGold['reason'] = '提现';
        $dealGold['last_balance'] = $user['gold'];// 会员余额
        return $dealGold;
    }

    // 准备提现记录数据
    private function withdrawRecord($withdraw, $user, $bank)
    {
        $withdrawRecord = [];
        $withdrawRecord['store_id'] = $user['store_id'];
        $withdrawRecord['user_id'] = $user['id'];
        $withdrawRecord['bank_name'] = $bank['bank_name'];
        $withdrawRecord['bank_code'] = $bank['bank_code'];
        $withdrawRecord['deposit'] = $bank['deposit'];
        $withdrawRecord['accepter_name'] = $bank['accepter_name'];
        $withdrawRecord['accepter_phone'] = $bank['accepter_phone'];
        $withdrawRecord['withdraw'] = $withdraw;
        $withdrawRecord['service_fee'] = $withdraw*0.006+100;
        return $withdrawRecord;
    }

    // 检查 提现数据是否合法 - 余额是否足够 / 银行卡是否存在 / 银行卡是否属于本人
    private function checkWithdraw($user, $bankId, $withdraw)
    {
        $gold = $user['gold']/100;
        if ($gold < $withdraw/100) {
            return $this->errorClient($gold, '当前可提现余额: '.$gold.' 元, 提现金额过大');// 返回实际可以提现金额
        }
        $BankLib = new BankLib;
        $bank = $BankLib->bankInfo($bankId);
        if ($bank['code'] != 20000 || !$bank['data']) {
            return $this->backJsonClient($bank['data'], '银行卡不存在');
        } elseif ($bank['data']['user_id'] != $user['id']) {
            return $this->errorClient($user['id'], '银行卡不属于当前登录用户');
        }
        return $bank;  
    }

    // 银行卡列表
    public function bankLists(Request $request)
    {
        $user = Auth::user()->toArray();
        $BankLib = new BankLib;
        return $BankLib->lists($user['id'], $user['bank_id']);
    }

    // 添加银行卡
    public function addBank(Request $request)
    {
        # store_id / user_id / 
        # bank_name / bank_code / deposit / accepter_name / accepter_phone
        
        $user = Auth::user()->toArray();
        $request['store_id'] = $user['store_id'];
        $request['user_id'] = $user['id'];
        $BankLib = new BankLib;
        return $BankLib->insertBank($request);
    }

    // 编辑银行卡
    public function editBank(Request $request)
    {
        $data = $request;// 需要有 地址ID
        $user = Auth::user()->toArray();
        $data['user_id'] = $user['id'];
        $data['bank_id'] = $user['bank_id'];
        $BankLib = new BankLib;
        return $BankLib->updateBank($data);
    }

    // 删除银行卡
    public function delBank(Request $request)
    {
        $id = $request['id'];
        $user = Auth::user()->toArray();
        $BankLib = new BankLib;
        return $BankLib->delBank($id, $user['id'], $user['bank_id']);
    }
}
